<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 14:15
 */

namespace addons\admin\controller;


use app\common\Code;
use app\common\Crypt;
use app\common\model\Admin;
use app\common\model\Sellers;
use app\common\model\Users;
use app\common\Session;

class User extends Common
{
    /**
     * 管理员进行登录
     * @return mixed
     */
    public function login()
    {
        $param = input();
        if (isset($param['name']) && isset($param['pwd'])) {
            if ($user = Users::get_user($param['name'])) {
                if (isset($user->password)) {
                    if ($user->password == Crypt::pwd($param['pwd'])) {
                        Session::login_success($user);
                        $this->error('前往主页', addon_url('admin://index/index'));
                    }
                }
            }
            $this->error('密码错误');
        }
        return $this->fetch();
    }

    /**
     * 退出
     */
    public function logout()
    {
        Session::logout();
        $this->success("退出成功", addon_url('admin://user/login'));
    }

    /**
     * 会员列表
     * @return mixed
     */
    public function user_list()
    {
        $this->check_login();
        $param = input();
        $list_ = Users::list_($param);
        $this->assign("param", $param);
        $this->assign("list_", $list_);
        return $this->fetch();
    }

    /**
     * 搜索用户身份列表
     */
    public function identity_list()
    {
        $this->check_login();
        $param = input();
        if(isset($param['search_']))
        {
            $list_ = Users::list_($param);
            foreach($list_ as $k=>$v)
            {
                $is_seller = Sellers::get_seller_by_user_id($v['id']);
                if($is_seller) { #此用户是商家
                    $list_[$k]['is_seller'] = 1;
                }else {         #此用户不是商家
                    $list_[$k]['is_seller'] = 0;
                }
                $is_admin = Admin::get_admin_by_user_id($v['id']);
                if($is_admin) { #此用户是管理员
                    $list_[$k]['is_admin'] = 1;
                }else {        #此用户不是管理员
                    $list_[$k]['is_admin'] = 0;
                }
            }
            $this->assign("list_", $list_);
        }
        $this->assign("param", $param);
        return $this->fetch();
    }

    /**
     * 会员设置成商家/取消商家
     */
    public function set_seller()
    {
        $this->check_login();
        $param = input();
        if($param['set_type'] == "set")  #设置商家
        {
            $is_seller = Sellers::get_seller_by_user_id($param['user_id']);
            if($is_seller) {     #请勿重复设置商家
                response_json(Code::EXISTS,Code::msg(Code::EXISTS));
            }
            Sellers::add_seller($param);  #设置商家
        }else if($param['set_type'] == "unset")      #取消商家设置
        {
            Sellers::del_seller($param['user_id']);
        }
        response_json(Code::NONE,Code::msg(Code::NONE));
    }

    /**
     * 会员设置成管理员/取消管理员
     */
    public function set_admin()
    {
        $this->check_login();
        $param = input();
        if($param['set_type'] == "set")
        {
            $is_admin = Admin::get_admin_by_user_id($param['user_id']);
            if($is_admin) {    #请勿重复设置管理员
                response_json(Code::EXISTS,Code::msg(Code::EXISTS));
            }
            Admin::add_admin($param);  #设置管理员
        }else
        {
            Admin::del_admin($param['user_id']);
        }
        response_json(Code::NONE,Code::msg(Code::NONE));
    }


}